package com.my.screenadapter.net.retrofit;


import com.ihsanbal.logging.Level;
import com.ihsanbal.logging.LoggingInterceptor;
import com.my.screenadapter.BuildConfig;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.platform.Platform;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zyj
 */
public class RetrofitUtil {

    private static ClientService mRetrofitClient;
    private static ClientService mRetrofitClientStr;

    public static ClientService getRetrofitGson() {
        if (mRetrofitClient == null) {
            ClientService mRetrofitClients = new Retrofit.Builder()
                    .baseUrl(ClientService.Base_IP)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(ClientService.class);
            return mRetrofitClients;
        }
        return mRetrofitClient;
    }

    public static ClientService getRetrofitStr() {
        if (mRetrofitClientStr == null) {
            okhttp3.OkHttpClient.Builder httpClientBuilder = new okhttp3.OkHttpClient.Builder();
            //add log record
            if (BuildConfig.DEBUG) {
            //打印网络请求日志
                LoggingInterceptor httpLoggingInterceptor = new LoggingInterceptor.Builder()
                        .loggable(BuildConfig.DEBUG)
                        .setLevel(Level.BASIC)
                        .log(Platform.INFO)
                        .request("Request")
                        .response("Response")
                        .build();
                httpClientBuilder.addInterceptor(httpLoggingInterceptor);
            }

            int DEFAULT_TIMEOUT = 60;
            httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request originalRequest = chain.request();
                            Request.Builder requestBuilder = originalRequest.newBuilder()
                                    .header("Content-Type", "application/json;charset=UTF-8")
                                    .header("Accept", "application/json")
                                    .header("Accept-Encoding", "gzip")
                                    .method(originalRequest.method(), originalRequest.body());
                            Request request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    });

            ClientService mRetrofitClients = new Retrofit.Builder()
                    .baseUrl(ClientService.Base_IP)
                    //添加string支持
                    .addConverterFactory(StringConverterFactory.create())

                    //gson需要添加在最后
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClientBuilder.build())
                    .build()
                    .create(ClientService.class);
            return mRetrofitClients;
        }
        return mRetrofitClientStr;
    }

    public static List<MultipartBody.Part> filesToMultipartBodyParts(List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-lifecircle"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }
}
